package com.kygl.teacher.dao.imp;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;



import com.kygl.auxwork.vo.AuxInfo;
import com.kygl.auxwork.vo.TaskAux;
import com.kygl.salary.vo.Salary;
import com.kygl.teacher.dao.*;
import com.kygl.teacher.vo.CourseRegister;
import com.kygl.teacher.vo.Dept;
import com.kygl.teacher.vo.Teacher;
import com.kygl.teacher.vo.TeacherAllInfo;
public class TeacherDaoImp extends HibernateDaoSupport implements ITeacherDao{

	public boolean addTeacher(Teacher t) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().save(t);
			return true;

		} catch (RuntimeException re) {
			throw re;
		}
		
	}

	public boolean delTeacher(Teacher teacher) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().delete(teacher);
			return true;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public boolean delTeacherById(int id) {
		// TODO Auto-generated method stub
		try {

			this.getHibernateTemplate().delete(this.getHibernateTemplate().get(
					"com.kygl.teacher.vo.Teacher", id));
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Teacher> findAllTeacher() {
		// TODO Auto-generated method stub
		List<Teacher> list = this.getHibernateTemplate().loadAll(Teacher.class);
		return list;
	}

	public Teacher findTeacherById(int id) {
		// TODO Auto-generated method stub
		return (Teacher) this.getHibernateTemplate().get(
				"com.kygl.teacher.vo.Teacher", id);
	}

	@SuppressWarnings("unchecked")
	public boolean findTeacherByName(String name) {
		// TODO Auto-generated method stub
		String hql = "from Teacher a where a.name=?";

		List<Teacher> list = this.getHibernateTemplate().find(hql, name.trim().toLowerCase());

		if (list.isEmpty()) {
			return false;
		} else {
			return true;
		}
	}

	@SuppressWarnings("unchecked")
	public int findTeacherByNameAndPassword(String name, String password) {
		// TODO Auto-generated method stub
		String hql = "from Teacher a where a.name=?";
		
		List<Teacher> list = this.getHibernateTemplate().find(hql, name.trim().toLowerCase());
		if (!list.isEmpty()) {
			for (Teacher iteacher : list) {
				if (iteacher.getPasswd()!=null  && iteacher.getPasswd().equals(password)) {
					return iteacher.getId();
				}
			}
		}
		return 0;
	}

	public boolean modifyTeacher(Teacher t) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().saveOrUpdate(t);
			return true;
		} catch (RuntimeException re) {

			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public int findTeacherByWidAndPassword(int wid,String password)
	{
		String hql = "from Teacher a where a.workId=?";
		
		List<Teacher> list = this.getHibernateTemplate().find(hql, wid);
		if (!list.isEmpty()) {
			for (Teacher iteacher : list) {
				if (iteacher.getPasswd()!=null  && iteacher.getPasswd().equals(password)) {
					return iteacher.getId();
				}
			}
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	public Teacher findTeacherById(int deptId, int workId) {

		final String hql2 = "from Teacher a where a.deptId="+deptId+"and a.workId="+workId;
	
		HibernateCallback hc = new HibernateCallback() {

			
			public Object doInHibernate(org.hibernate.Session session)
					throws HibernateException, SQLException {
	
				Query query = session.createQuery(hql2);
		

				List<Teacher> testlist = query.list();
				//List testlist = query.list();
				if (testlist.isEmpty()) {
					 System.out.println("no teacher");
				}
//				Iterator it = testlist.iterator();
//				while(it.hasNext()){
//					Object[] obj=(Object[]) it.next(); 
//					int aa = (Integer)(obj[0]);
//					int bb = (Integer)(obj[1]);
//					System.out.println("\naa"+aa+"bb"+bb);
//				}
				return testlist;
			}

		};
		
		List<Teacher> testlist = getHibernateTemplate().executeFind(hc);
		if (!testlist.isEmpty()) {
			for (Teacher iteacher : testlist) {
				return iteacher;
			}
		}
		
//		String hql = "from Teacher a where a.workId=?";
//		List<Teacher> list = this.getHibernateTemplate().find(hql,workId);
//		if (!list.isEmpty()) {
//			for (Teacher iteacher : list) {
//				if (iteacher.getDeptId()!= null  && iteacher.getDeptId() == deptId) 
//				return iteacher;
//			}
//		}

		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List findTeacherAllInfo()
	{
		final String hql = "from Teacher a,Salary b where a.kind = b.kind and a.rank = b.rank";
		HibernateCallback hc = new HibernateCallback() {

			public Object doInHibernate(org.hibernate.Session session)
					throws HibernateException, SQLException {
	
				Query query = session.createQuery(hql);
				List testlist = query.list();
				List<TeacherAllInfo> teacherAllInfoList = new ArrayList<TeacherAllInfo>();
				//List testlist = query.list();
				if (testlist.isEmpty()) {
					 System.out.println("no teacher for eva!");
				} else {
					Iterator it = testlist.iterator();
					while(it.hasNext()){
						Teacher t = new Teacher();
						Salary s = new Salary();
						
						
						Object[] obj=(Object[]) it.next(); 
						t = (Teacher)obj[0];
						s = (Salary)obj[1];
						
						TeacherAllInfo teacherAllInfo = new TeacherAllInfo(t,s);
						teacherAllInfoList.add(teacherAllInfo);
						
					}
				}

				return teacherAllInfoList;
			}

		};
		List<TeacherAllInfo> list= getHibernateTemplate().executeFind(hc);
		return list;
	}
	
	public List<TeacherAllInfo> findAllTeacherAllByScore(){
		
		final String hql = "from Teacher a, Dept b,Salary c where a.deptId = b.id " +
				" and a.kind = c.kind and a.rank = c.rank " +
				" order by a.kind desc";
		HibernateCallback hc = new HibernateCallback() {

			public Object doInHibernate(org.hibernate.Session session)
					throws HibernateException, SQLException {
	
				Query query = session.createQuery(hql);
				List testlist = query.list();
				List<TeacherAllInfo> teacherAllInfoList = new ArrayList<TeacherAllInfo>();
				//List testlist = query.list();
				if (testlist.isEmpty()) {
					 System.out.println("no teacher for eva!");
				} else {
					Iterator it = testlist.iterator();
					while(it.hasNext()){
						Teacher t = new Teacher();
						Dept d = new Dept();
						Salary s = new Salary();
						
						
						Object[] obj=(Object[]) it.next(); 
						t = (Teacher)obj[0];
						d = (Dept)obj[1];
						s = (Salary)obj[2];
						
						TeacherAllInfo teacherAllInfo = new TeacherAllInfo(t,s);
						teacherAllInfo.setDept(d);
						teacherAllInfoList.add(teacherAllInfo);
						
					}
				}

				return teacherAllInfoList;
			}

		};
		List<TeacherAllInfo> list= getHibernateTemplate().executeFind(hc);
		return list;
	}
}
